package _Pan3D.role
{
	import _Pan3D.core.Mathclass;
	import _Pan3D.particle.FireBall;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.FireBallShader;
	
	import _me.Scene_data;
	
	import flash.display3D.Context3D;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	public class Npc extends Role
	{
		public var life:int = 100;
		private var _death:Boolean;
		public var target:Role;
		private var _fire:FireBall;
		
		public function Npc(context:Context3D, info:RoleVo)
		{
			super(context, info);
		}
		public var moveRec:Rectangle;
		private var timeId:uint;
		
		public function action_move():void{
			//this.x = moveRec.x;
			//this.y = moveRec.y;
			var moveAry:Vector.<Vector3D> = new Vector.<Vector3D>;
			
			for(var i:int;i<10;i++){
				var vec3d:Vector3D = new Vector3D(moveRec.x + moveRec.width*Math.random(),0,moveRec.y + moveRec.height*Math.random());
				moveAry.push(vec3d);
			}
	
			if(Math.random() > 0.5){
				seepNum = 0.5;
				this.pushLoadArr(moveAry,RoleVo.RUN);
			}else{
				seepNum = 0.25;
				this.pushLoadArr(moveAry,RoleVo.WALK);
			}
			timeId = setTimeout(action_move,45000);
		}
		/*
		public function action_attack():void{
			this.attack("attack");
			timeId = setTimeout(action_death,5000);
		}
		public function action_death():void{
			this.death();
			timeId = setTimeout(action_move,2000);
		}
		*/
		public function clearAI():void{
			clearTimeout(timeId);
		}
		
		public function lifeDead():void{
			if(!this._death){
				this.death();
			}
			this._death = true;
		}
		
		override public function playOver():void{
			//super.playOver();
			if(this._death){
				//setTimeout(reset,2000);
				reset();
			}
		}
		
		public function reset():void{
			this.life = 100;
			action_move();
			this.x = 2000*Math.random();
			this.z = 2000*Math.random();
			this._death = false;
		}
		
		public function magicFire(tg:Role):void{
			if(!_fire){
				_fire = new FireBall(this._context);
				_fire.program = Program3DManager.getInstance().getProgram(FireBallShader.FIREBALLSHADER);
				Scene_data.skillLevel.addSkill(_fire);
			}
			_fire.x = this.x;
			_fire.y = this.y;
			_fire.z = this.z;
			this.target = tg;
			this.attack("attack2");
			var angly:Number = Mathclass.math_angle(tg.x, tg.z, this.x, this.z)
			this.rotationY = 90 - angly;
		}
		//private var baseVector3D:Vector3D = new Vector3D();
		override public function update():void{
			super.update();
			if(target){
				var xx:Number = target.x - _fire.x;
				var yy:Number = target.y - _fire.y + 10;
				var zz:Number = target.z - _fire.z;
				var rr:Number = Math.sqrt(xx*xx+yy*yy+zz*zz);
				//trace("rr" + rr)
				if(rr < 1){
					Scene_data.skillLevel.removeSkill(_fire);
					this.target = null;
				}else{
					_fire.setPos(_fire.x + xx/rr*_fire.speed,_fire.y + yy/rr*_fire.speed,_fire.z + zz/rr*_fire.speed);
				}
			}
		}
		
		
		
	}
}